package cn.edu.hznu.ocw.actions;

import java.io.File;
import java.io.FileFilter;

import org.apache.struts2.ServletActionContext;

import com.googlecode.jsonplugin.annotations.JSON;

public class GetSectionFilesAction extends OCWAction {
	private static final long serialVersionUID = 7424417487367504235L;
	// http://localhost:8080/ocw/getSectionFiles.action?courseType=coursetype&subType=subtype&lecturer=lecturer&courseName=coursename&sectionName=section_1
	private String courseType;
	private String subType;
	private String lecturer;
	private String courseName;
	private String sectionName;

	private String[] fileNames = null;

	@Override
	public String execute() throws Exception {
		StringBuilder realPath = new StringBuilder(ServletActionContext
				.getServletContext().getRealPath("/"));
		realPath.append("_Courses\\").append(courseType).append("\\").append(
				subType).append("\\").append(lecturer).append("\\").append(
				courseName).append("\\").append(sectionName);
		//System.out.println(realPath);
		File folder = new File(realPath.toString());
		File[] file = folder.listFiles(new FileFilter() {
			@Override
			public boolean accept(File file) {
				if (file.exists() && file.isFile() && !file.isHidden()) {
					return true;
				}
				return false;
			}
		});
		fileNames = new String[file.length];
		for (int i = 0; i < file.length; i++) {
			fileNames[i] = file[i].getName();
		}
		return SUCCESS;
	}

	public void setCourseType(String courseType) {
		this.courseType = courseType;
	}

	public void setSubType(String subType) {
		this.subType = subType;
	}

	public void setLecturer(String lecturer) {
		this.lecturer = lecturer;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}

	public void setSectionName(String sectionName) {
		this.sectionName = sectionName;
	}

	@JSON(name = "FileNames")
	public String[] getFileNames() {
		return fileNames;
	}
}
